package com.wzp.yyptproject.service;

import com.wzp.yyptproject.entity.*;
import com.wzp.yyptproject.repository.DoctorRepository;
import com.wzp.yyptproject.repository.EkBingRepository;
import com.wzp.yyptproject.repository.PingLunRepository;
import com.wzp.yyptproject.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Transactional
@Service
@Slf4j
public class PingLunServiceimp implements PingLunService{

    @Autowired
   private PingLunRepository pingLunRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private DoctorRepository doctorRepository;
    @Override
    public  List<PingLun>  finfByUid (Integer uid) {

        return pingLunRepository.findAndByUid(uid);
    }


    @Override
    public List<PingLun> findAllPinglun() {
        List<PingLun> pingLunList =pingLunRepository.findAll();
        return pingLunList;
    }

    @Override
    public List<PingLun> findAllByStatus() {
        List<PingLun> pingLunList =pingLunRepository.findAndByPstatus(1);
        return pingLunList;
    }


    @Override
    public List<PingLun2> findAllPinglun1(List<PingLun> list) {
        List<PingLun2> list1=new ArrayList<>();
        for (PingLun pingLun:list){
            PingLun2  pingLun2=new PingLun2();
            pingLun2.setPid(pingLun.getPid());
            pingLun2.setPdate(pingLun.getPdate());
            pingLun2.setPxingji(pingLun.getPxingji());
            pingLun2.setPcontent(pingLun.getPcontent());
            pingLun2.setPstatus(pingLun.getPstatus());
            pingLun2.setUUid(pingLun.getUid());
            User user = userRepository.findAndByUUid(pingLun.getUid());
            pingLun2.setUidcard(user.getUidcard());
            pingLun2.setUidcard(user.getUname());
            pingLun2.setUaddress(user.getUaddress());
            pingLun2.setUhead(user.getUhead());
            pingLun2.setUnicheng(user.getUnicheng());
            pingLun2.setDDid(pingLun.getDid());
            Doctor doctor = doctorRepository.findAndByDDid(pingLun.getDid());
            pingLun2.setDDname(doctor.getDDname());
            pingLun2.setDTitle(doctor.getDTitle());


            list1.add(pingLun2);
        }
        return list1;
    }

    @Override
    public boolean addPinglun(String uidcard, String uname,Integer ddid, Date pdate, Integer pxingji, String pcontent) {
        PingLun pingLun = new PingLun();

        pingLun.setDid(ddid);
        pingLun.setPcontent(pcontent);
        User user=userRepository.findAndByUnameAndUidcard(uname,uidcard);
        pingLun.setUid(user.getUUid());
        pingLun.setPdate(pdate);
        pingLun.setPxingji(pxingji);
        pingLun.setPstatus(0);
        pingLunRepository.save(pingLun);
        return true;

    }
    public Page<PingLun2> findByPage(int pageCode, int pageSize) {
        System.out.println(pageCode);
        System.out.println(pageSize);
        Pageable pageable = new PageRequest(pageCode, pageSize);
        Page<PingLun> doctors=pingLunRepository.findAll(pageable);
        System.out.println("cccc"+doctors.getTotalElements());
        List<PingLun2> list=findAllPinglun1(doctors.getContent());
        System.out.println(list);
        List<PingLun2> list1=new ArrayList<>();
        for(PingLun2 pingLun2 :list){
            if (pingLun2.getPstatus()==1){
                PingLun2 pingLun21=new PingLun2();
                pingLun21.setUidcard(pingLun2.getUidcard());
                pingLun21.setDTitle(pingLun2.getDTitle());
                pingLun21.setDDname(pingLun2.getDDname());
                pingLun21.setDDid(pingLun2.getDDid());
                pingLun21.setUnicheng(pingLun2.getUnicheng());
                pingLun21.setUname(pingLun2.getUname());
                pingLun21.setUhead(pingLun2.getUhead());
                pingLun21.setUUid(pingLun2.getUUid());
                pingLun21.setUaddress(pingLun2.getUaddress());
                pingLun21.setPcontent(pingLun2.getPcontent());
                pingLun21.setPdate(pingLun2.getPdate());
                pingLun21.setPid(pingLun2.getPid());
                pingLun21.setPxingji(pingLun2.getPxingji());
                pingLun21.setPstatus(pingLun2.getPstatus());
                list1.add(pingLun21);
            }
        }
        return new PageImpl<>(list1);
    }
    public  List<PingLun>  findAllMyPingLun (String uname,String uidcard) {
        User user = userRepository.findAndByUnameAndUidcard(uname,uidcard);
        return pingLunRepository.findAndByUid(user.getUUid());
    }

    @Override
    public Integer findAllCount() {
        List<PingLun> pingLunList = pingLunRepository.findAndByPstatus(1);
        return pingLunList.size();
    }

    @Override
    public boolean deleteByPid(Integer pid) {
        pingLunRepository.deleteById(pid);
        return true;
    }
}
